class Solution {
public:
    int maxCoins(vector<int>& piles)
    {
        ranges::sort(piles);
        int n = piles.size() / 3;
        int ans = 0;
        for (int i = piles.size() - 2; --n >= 0 && i >= 0; i -= 2)
            ans += piles[i];
        return ans;
    }
};